package com.lmiot.xyclick.App;

import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.service.wallpaper.WallpaperService;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import com.lmiot.xyclick.Util.DoubleUtils;
import com.lmiot.xyclick.Util.LogUtil;
import com.youyi.yypermissionlibrary.SDK.OnPerListener;
import com.youyi.yypermissionlibrary.SDK.YYPerUtils;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;

public class LiveWallpaperService extends WallpaperService {
    private static final String TAG = "LiveWallpaperService";
    private final long delayTime = 300;
    private long lastClickTime = 0;
    private GestureDetector mGestureDetector;

    public Engine onCreateEngine() {
        return new WallpaperEngine(this);
    }

    public class WallpaperEngine extends Engine {
        private Context context;

        public WallpaperEngine(LiveWallpaperService liveWallpaperService) {
            super();
            this.context = liveWallpaperService;
        }

        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            setTouchEventsEnabled(true);
            LiveWallpaperService.this.mGestureDetector = new GestureDetector(this.context, new G());
        }

        public void onTouchEvent(MotionEvent motionEvent) {
            super.onTouchEvent(motionEvent);
            LiveWallpaperService.this.mGestureDetector.onTouchEvent(motionEvent);
        }

        public Bundle onCommand(String str, int i, int i2, int i3, Bundle bundle, boolean z) {
            if (Build.VERSION.SDK_INT >= 34 && "android.wallpaper.tap".equals(str)) {
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis - LiveWallpaperService.this.lastClickTime > 300) {
                    LiveWallpaperService.this.lastClickTime = currentTimeMillis;
                } else {
                    LogUtil.d(LiveWallpaperService.TAG, "双击桌面方式一:" + i + ":" + i2 + ":" + str);
                    int i4 = MyApp.mWidth / 2;
                }
            }
            return super.onCommand(str, i, i2, i3, bundle, z);
        }

        public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
            super.onSurfaceCreated(surfaceHolder);
        }

        public void showWall() {
            Bitmap bitmap;
            try {
                Canvas lockCanvas = getSurfaceHolder().lockCanvas();
                if (lockCanvas != null) {
                    String saveBitmap = DoubleUtils.getSaveBitmap(MyApp.getContext());
                    if (TextUtils.isEmpty(saveBitmap)) {
                        LogUtil.d(LiveWallpaperService.TAG, "使用壁纸0001");
                        bitmap = creatBitamp();
                        DoubleUtils.setSaveBitmap(MyApp.getContext(), DoubleUtils.bitmapToString(bitmap));
                    } else {
                        LogUtil.d(LiveWallpaperService.TAG, "使用壁纸002");
                        bitmap = DoubleUtils.stringToBitMap(saveBitmap);
                    }
                    if (bitmap != null) {
                        lockCanvas.drawBitmap(bitmap, 0.0f, 0.0f, new Paint());
                    } else {
                        lockCanvas.drawColor(-1);
                    }
                    getSurfaceHolder().unlockCanvasAndPost(lockCanvas);
                }
            } catch (Exception unused) {
            }
        }

        public void showWallBitmap(String str) {
            try {
                Canvas lockCanvas = getSurfaceHolder().lockCanvas();
                if (lockCanvas != null) {
                    Bitmap stringToBitMap = DoubleUtils.stringToBitMap(str);
                    if (stringToBitMap != null) {
                        lockCanvas.drawBitmap(stringToBitMap, 0.0f, 0.0f, new Paint());
                    } else {
                        lockCanvas.drawColor(-1);
                    }
                    getSurfaceHolder().unlockCanvasAndPost(lockCanvas);
                }
            } catch (Exception unused) {
            }
        }

        @SuppressLint("MissingPermission")
        public Bitmap creatBitamp() {
            if (Build.VERSION.SDK_INT >= 24) {
                try {
                    return drawableToBitmap(WallpaperManager.getInstance(LiveWallpaperService.this.getApplicationContext()).getDrawable());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        public Bitmap drawableToBitmap(Drawable drawable) {
            Bitmap createBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(createBitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            drawable.draw(canvas);
            return createBitmap;
        }

        public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) {
            super.onSurfaceDestroyed(surfaceHolder);
        }

        public void onDestroy() {
            super.onDestroy();
        }

        public void onVisibilityChanged(boolean z) {
            super.onVisibilityChanged(z);
            if (!z) {
                return;
            }
            if (YYPerUtils.hasImage()) {
                showWall();
                return;
            }
            String saveBitmap = DoubleUtils.getSaveBitmap(MyApp.getContext());
            if (!TextUtils.isEmpty(saveBitmap)) {
                showWallBitmap(saveBitmap);
            } else {
                YYPerUtils.image(new OnPerListener() {
                    /* class com.lmiot.xyclick.App.LiveWallpaperService.WallpaperEngine.AnonymousClass1 */

                    @Override // com.youyi.yypermissionlibrary.SDK.OnPerListener
                    public void result(boolean z, String str) {
                        if (z) {
                            WallpaperEngine.this.showWall();
                        } else {
                            ToastUtil.err("缺少必要权限");
                        }
                    }
                });
            }
        }
    }

    class G extends GestureDetector.SimpleOnGestureListener {
        G() {
        }

        public boolean onDoubleTap(MotionEvent motionEvent) {
            if (Build.VERSION.SDK_INT < 34) {
                float x = motionEvent.getX();
                int i = MyApp.mWidth / 2;
                LogUtil.d(LiveWallpaperService.TAG, "双击桌面方式二():" + x + ":" + motionEvent.getY());
            }
            return super.onDoubleTap(motionEvent);
        }
    }
}
